<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>SparseVector.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="SparseVector code in Java">
<meta NAME="TITLE" CONTENT="SparseVector code in Java">
<meta NAME="KEYWORDS" CONTENT="SparseVector,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>SparseVector.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "SparseVector.java">SparseVector.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac SparseVector.java</span>
<span class="comment"> *  Execution:    java SparseVector</span>
<span class="comment"> *  Dependencies: StdOut.java</span>
<span class="comment"> *  </span>
<span class="comment"> *  A sparse vector, implementing using a symbol table.</span>
<span class="comment"> *</span>
<span class="comment"> *  [Not clear we need the instance variable N except for error checking.]</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">SparseVector</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a </span><span class="keyword">&lt;em&gt;</span><span class="comment">d</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-dimensional mathematical vector.</span>
<span class="comment"> *  Vectors are mutable: their values can be changed after they are created.</span>
<span class="comment"> *  It includes methods for addition, subtraction,</span>
<span class="comment"> *  dot product, scalar product, unit vector, and Euclidean norm.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The implementation is a symbol table of indices and values for which the vector</span>
<span class="comment"> *  coordinates are nonzero. This makes it efficient when most of the vector coordindates</span>
<span class="comment">  * are zero.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,    </span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/35applications"</span><span class="keyword">&gt;</span><span class="comment">Section 3.5</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne. </span>
<span class="comment"> *  See also {</span><span class="type">@link</span><span class="comment"> Vector} for an immutable (dense) vector data type.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">SparseVector</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> d</span><span class="symbol">;</span><span class="normal">                   </span><span class="comment">// dimension</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">ST&lt;Integer, Double&gt;</span><span class="normal"> st</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// the vector, represented by index-value pairs</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Initializes a d-dimensional zero vector.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> d the dimension of the vector</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">SparseVector</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> d</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">d  </span><span class="symbol">=</span><span class="normal"> d</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> ST</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">,</span><span class="normal"> Double</span><span class="symbol">&gt;();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Sets the ith coordinate of this vector to the specified value.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  i the index</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  value the new value</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless i is between 0 and d-1</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> value</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> i </span><span class="symbol">&gt;=</span><span class="normal"> d</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Illegal index"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">value </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">)</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">delete</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">              st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> value</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns the ith coordinate of this vector.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  i the index</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the value of the ith coordinate of this vector</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IndexOutOfBoundsException unless i is between 0 and d-1</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> i </span><span class="symbol">&gt;=</span><span class="normal"> d</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Illegal index"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">st</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of nonzero entries in this vector.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of nonzero entries in this vector</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">nnz</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns the dimension of this vector.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the dimension of this vector</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> #dimension()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> d</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns the dimension of this vector.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the dimension of this vector</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">dimension</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> d</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the inner product of this vector with the specified vector.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  that the other vector</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the dot product between this vector and that vector</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the lengths of the two vectors are not equal</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">dot</span><span class="symbol">(</span><span class="usertype">SparseVector</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">d </span><span class="symbol">!=</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">d</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Vector lengths disagree"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> sum </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// iterate over the vector with the fewest nonzeros</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&lt;=</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">:</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">that</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> sum </span><span class="symbol">+=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal">  </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">:</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> sum </span><span class="symbol">+=</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> sum</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the inner product of this vector with the specified array.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  that the array</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the dot product between this vector and that array</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the dimensions of the vector and the array are not equal</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">dot</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> sum </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">:</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span>
<span class="normal">            sum </span><span class="symbol">+=</span><span class="normal"> that</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">*</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> sum</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the magnitude of this vector.</span>
<span class="comment">     * This is also known as the L2 norm or the Euclidean norm.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the magnitude of this vector</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">magnitude</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">sqrt</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">dot</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the Euclidean norm of this vector.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the Euclidean norm of this vector</span>
<span class="comment">     * </span><span class="type">@deprecated</span><span class="comment"> Replaced by {</span><span class="type">@link</span><span class="comment"> #magnitude()}.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">norm</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">sqrt</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="function">dot</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the scalar-vector product of this vector with the specified scalar.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  alpha the scalar</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the scalar-vector product of this vector with the specified scalar</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">SparseVector</span><span class="normal"> </span><span class="function">scale</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> alpha</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">SparseVector</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SparseVector</span><span class="symbol">(</span><span class="normal">d</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">:</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span><span class="normal"> c</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> alpha </span><span class="symbol">*</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> c</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the sum of this vector and the specified vector.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  that the vector to add to this vector</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the sum of this vector and that vector</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the dimensions of the two vectors are not equal</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">SparseVector</span><span class="normal"> </span><span class="function">plus</span><span class="symbol">(</span><span class="usertype">SparseVector</span><span class="normal"> that</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">d </span><span class="symbol">!=</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">d</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Vector lengths disagree"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">SparseVector</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SparseVector</span><span class="symbol">(</span><span class="normal">d</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">:</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span><span class="normal"> c</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">this</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">));</span><span class="normal">                </span><span class="comment">// c = this</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">:</span><span class="normal"> that</span><span class="symbol">.</span><span class="normal">st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span><span class="normal"> c</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> that</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> c</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">));</span><span class="normal">     </span><span class="comment">// c = c + that</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> c</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns a string representation of this vector.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a string representation of this vector, which consists of the </span>
<span class="comment">     *         the vector entries, separates by commas, enclosed in parentheses</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">toString</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">StringBuilder</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">StringBuilder</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">:</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            s</span><span class="symbol">.</span><span class="function">append</span><span class="symbol">(</span><span class="string">"("</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> i </span><span class="symbol">+</span><span class="normal"> </span><span class="string">", "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">") "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> s</span><span class="symbol">.</span><span class="function">toString</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">SparseVector</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">SparseVector</span><span class="normal"> a </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SparseVector</span><span class="symbol">(</span><span class="number">10</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">SparseVector</span><span class="normal"> b </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">SparseVector</span><span class="symbol">(</span><span class="number">10</span><span class="symbol">);</span>
<span class="normal">        a</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="number">3</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.50</span><span class="symbol">);</span>
<span class="normal">        a</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="number">9</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.75</span><span class="symbol">);</span>
<span class="normal">        a</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="number">6</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.11</span><span class="symbol">);</span>
<span class="normal">        a</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="number">6</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.00</span><span class="symbol">);</span>
<span class="normal">        b</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="number">3</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.60</span><span class="symbol">);</span>
<span class="normal">        b</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="number">4</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.90</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"a = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> a</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"b = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> b</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"a dot b = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> a</span><span class="symbol">.</span><span class="function">dot</span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"a + b   = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> a</span><span class="symbol">.</span><span class="function">plus</span><span class="symbol">(</span><span class="normal">b</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
